//
//  DSEMainVC.m
//  qqqqqq
//
//  Created by iOSer on 2017/3/15.
//  Copyright © 2017年 iOSer. All rights reserved.
//

#import "LEMainVC.h"
#import "GGAppDelegate.h"
#import "GGNavViewcontroller.h"
#import "LEPlayVC.h"
#import "LEUserInfoCoreDataManager.h"
@interface LEMainVC ()
@property(nonatomic,strong) UIButton *backBtn;
@property(nonatomic,strong) UILabel *titleLab;
@property(nonatomic,strong) UIButton *reStartBtn;
@property(nonatomic,strong) UIButton *readArchivesBtn;
@end

@implementation LEMainVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view setBackgroundColor:HEXColor(0xffffff, 1)];
    GGAppDelegate * appDelegate = (GGAppDelegate *)[UIApplication sharedApplication].delegate;
    [self.navigationController setNavigationBarHidden:YES animated:YES];
    appDelegate.allowRotation = YES;//(以上2行代码,可以理解为打开横屏开关)
    
    [self setNewOrientation:YES];//调用转屏代码
    // Do any additional setup after loading the view.
}

-(void)viewWillAppear:(BOOL)animated{
    [self setupUI];
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    [UIView animateWithDuration:1.5 animations:^{
        self.reStartBtn.alpha = 1;
        self.readArchivesBtn.alpha = 1;
        self.backBtn.alpha = 1;
    } completion:^(BOOL finished) {
        
    }];
    // 禁用 iOS7 返回手势
    if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
        self.navigationController.interactivePopGestureRecognizer.enabled = NO;
    }
}

-(void)viewWillDisappear:(BOOL)animated{
    [self.navigationController setNavigationBarHidden:NO animated:YES];
    self.navigationController.interactivePopGestureRecognizer.enabled = YES;
}
-(void)setupUI{
    [self.view addSubview:self.titleLab];
    [self.view addSubview:self.reStartBtn];
    [self.view addSubview:self.readArchivesBtn];
    [self.view addSubview:self.backBtn];
    [self.titleLab makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).offset(GGRealValue(20));
        make.left.right.equalTo(self.view);
    }];
    [_reStartBtn makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.view).offset(-GGRealValue(30));
        make.left.equalTo(self.view).offset(20);
        make.size.equalTo(CGSizeMake((SCREEN_WIDTH-80)/3, GGRealValue(30)));
    }];
    [_readArchivesBtn makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.view).offset(-GGRealValue(30));
        make.left.equalTo(self.reStartBtn.mas_right).offset(20);
        make.size.equalTo(CGSizeMake((SCREEN_WIDTH-80)/3, GGRealValue(30)));
    }];
    [_backBtn makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.view).offset(-GGRealValue(30));
        make.left.equalTo(self.readArchivesBtn.mas_right).offset(20);
        make.size.equalTo(CGSizeMake((SCREEN_WIDTH-80)/3, GGRealValue(30)));
    }];
    
}
-(void)backBtnClick{
    GGAppDelegate * appDelegate = (GGAppDelegate *)[UIApplication sharedApplication].delegate;
    
    appDelegate.allowRotation = NO;//关闭横屏仅允许竖屏
    
    [self setNewOrientation:NO];
    
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)setNewOrientation:(BOOL)fullscreen

{
    
    if (fullscreen) {
        
        NSNumber *resetOrientationTarget = [NSNumber numberWithInt:UIInterfaceOrientationUnknown];
        
        [[UIDevice currentDevice] setValue:resetOrientationTarget forKey:@"orientation"];
        
        
        
        NSNumber *orientationTarget = [NSNumber numberWithInt:UIInterfaceOrientationLandscapeLeft];
        
        [[UIDevice currentDevice] setValue:orientationTarget forKey:@"orientation"];
        
    }else{
        
        NSNumber *resetOrientationTarget = [NSNumber numberWithInt:UIInterfaceOrientationUnknown];
        
        [[UIDevice currentDevice] setValue:resetOrientationTarget forKey:@"orientation"];
        
        
        
        NSNumber *orientationTarget = [NSNumber numberWithInt:UIInterfaceOrientationPortrait];
        
        [[UIDevice currentDevice] setValue:orientationTarget forKey:@"orientation"];
        
    }
    
}
-(void)readArchivesBtnClick{
    [[LEUserInfoCoreDataManager shareInstance] readContextWithtargetCallBack:^(LEUserInfoModel *userInfoModel) {
        LEPlayVC *playVC = [LEPlayVC new];
        playVC.loadingUserInfo = userInfoModel;
        playVC.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:playVC animated:YES];
    } withDefeatedBlock:^{
        [[GGShowHUDTool sharedInstance] showInfoHudWith:@"读读读！你他妈存进度了吗？"];
    }];
}

-(void)reStartBtnClick{
    LEPlayVC *playVC = [LEPlayVC new];
    playVC.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:playVC animated:YES];
}
-(UILabel *)titleLab{
    if (!_titleLab) {
        _titleLab = [[UILabel alloc] initWithTitle:@"屌丝浮生记" color:HEXColor(0x49515c, 0.6) sizeFont:GGRealValue(20)];
        _titleLab.textAlignment = NSTextAlignmentCenter;
    }
    return _titleLab;
}

-(UIButton *)reStartBtn{
    if (!_reStartBtn) {
        _reStartBtn = [UIButton new];
        [_reStartBtn setBackgroundColor:HEXColor(0x000000, 1)];
       
        [_reStartBtn setTitle:@"开始游戏" forState:UIControlStateNormal];
        _reStartBtn.alpha = 0;
        _reStartBtn.layer.masksToBounds = YES;
        [_reStartBtn addTarget:self action:@selector(reStartBtnClick) forControlEvents:UIControlEventTouchUpInside];
    }
    return _reStartBtn;
}

-(UIButton *)readArchivesBtn{
    if (!_readArchivesBtn) {
        _readArchivesBtn = [UIButton new];
        [_readArchivesBtn setBackgroundColor:HEXColor(0x000000, 1)];
        [_readArchivesBtn setTitle:@"读取进度" forState:UIControlStateNormal];
//        _readArchivesBtn.layer.cornerRadius = GGRealValue()/2;
        _readArchivesBtn.layer.masksToBounds = YES;
        _readArchivesBtn.alpha = 0;
        [_readArchivesBtn addTarget:self action:@selector(readArchivesBtnClick) forControlEvents:UIControlEventTouchUpInside];
    }
    return _readArchivesBtn;
}
-(UIButton *)backBtn{
    if (!_backBtn) {
        _backBtn = [UIButton new];
        _backBtn.alpha = 0;
        [_backBtn setBackgroundColor:HEXColor(0x000000, 1)];
        [_backBtn setTitle:@"退出游戏" forState:UIControlStateNormal];
        [_backBtn addTarget:self action:@selector(backBtnClick) forControlEvents:UIControlEventTouchUpInside];
    }
    return _backBtn;
}



@end
